
package trim.differentmaterials.resttrimmer;

import trim.differentmaterials.core.*;
import trim.differentmaterials.generator.core.*;
import trim.differentmaterials.generator.columns.*;

/**
 *
 * @author Podorov Alexander
 */

public class CompositeRestTrimmer extends RestTrimmer
{
    public CompositeRestTrimmer(TrimCore core, Generator generator)
    {
        super(core, generator);
    }

    protected void restsToPatterns() throws NoSolutionsException
    {
        while (isFormatRests() && isMaterialRests())
        {           
            RealPattern pattern = (RealPattern)generator.getResidualPattern(true);
            int minIndex = materialToIndex(pattern.getMaterial());

            materialRests[minIndex]--;

            int[] items = pattern.getItems();
            for (int i = 0; i < formatCount; i++)
            {
                formatRests[i] -= items[i];
            }
            ((RealPattern)pattern).setCount(1);
            patterns.add(pattern);            
        }

        if (isFormatRests())
        {
            throw new NoSolutionsException();
        }
    }

    public void start() throws NoSolutionsException
    {
        setMaterialRests();
        setFormatRests();
        restsToPatterns();
    }
}
